package net.jdm.domain.caculator;

import net.jdm.domain.entity.ContainerNode;

public class DistanceCaculator {
	private static final DistanceCaculator CACULATOR=new DistanceCaculator();
	protected DistanceCaculator(){
	}
    public static DistanceCaculator getInstance(){
		return CACULATOR;
	}
	public float instability(ContainerNode containerNode) {
		float totalCouplingWeight = containerNode.afferentCouplingWeight()
				+ containerNode.efferentCouplingWeight();
		if (totalCouplingWeight > 0) {
			return containerNode.efferentCouplingWeight() / totalCouplingWeight;
		}
		return 0;
	}

	public float abstractness(ContainerNode containerNode) {
		if (containerNode.getClassCount() > 0) {
			return (float) containerNode.getAbstractClassCount() / (float) containerNode.getClassCount();
		}

		return 0;
	}
	public float distance(ContainerNode containerNode) {
		float d = Math.abs((abstractness(containerNode) + instability(containerNode)-1));
		return d * (containerNode.isStable()?0:1);
	}
}
